// 公共JavaScript文件


// 用户认证管理
const AuthManager = {
    // 用户信息
    user_info: {
        name: '',
        code: '',
        user_type: ''
    },
    
    // 认证状态
    finished: true,
    
    // 获取用户信息
    get_user() {
        this.finished = true;
        return new Promise((resolve, reject) => {
            axios({
                method: 'get',
                url: '/demo1/get_user',
                params: {
                    url: window.location.pathname,
                },
            }).then(response => {
                console.log(response.data.status);
                if (response.data.status) {
                    this.user_info.name = response.data.url.name;
                    this.user_info.code = response.data.url.code;
                    this.user_info.user_type = response.data.user_type;
                    this.finished = false;
                    resolve(this.user_info);
                } else {
                    window.location.href = response.data.url;
                    reject(new Error('认证失败，重定向到登录页面'));
                }
            }).catch(error => {
                CommonUtils.showWarning("网络异常！");
                console.log(error);
                reject(error);
            });
        });
    },
    
    // 检查认证状态
    isAuthenticated() {
        return !this.finished;
    },
    
    // 获取用户信息
    getUserInfo() {
        return this.user_info;
    }
};

// 公共工具函数
const CommonUtils = {
    // 显示成功消息
    showSuccess(message) {
        const h = this.$createElement;
        this.$notify({
            title: '成功',
            message: h('i', { style: 'color: teal'}, message),
            type: 'success'
        });
    },
    
    // 显示警告消息
    showWarning(message) {
        this.$message({
            showClose: true,
            message: message,
            type: 'warning'
        });
    },
    
    // 显示错误消息
    showError(message) {
        this.$message({
            showClose: true,
            message: message,
            type: 'error'
        });
    },
    
    // 显示信息消息
    showInfo(message) {
        const h = this.$createElement;
        this.$notify({
            title: '提示',
            message: h('i', { style: 'color: teal'}, message)
        });
    },
    
    // 格式化时间
    formatTime(date) {
        if (!date) return '';
        const d = new Date(date);
        return d.toLocaleString('zh-CN', {
            year: 'numeric',
            month: '2-digit',
            day: '2-digit',
            hour: '2-digit',
            minute: '2-digit',
            second: '2-digit'
        });
    },
    
    // 计算时间差
    getTimeDiff(clientTime, serverTime) {
        if (!clientTime || !serverTime) return 0;
        const client = new Date(clientTime);
        const server = new Date(serverTime);
        return Math.abs((server - client) / 1000);
    },
    
    // 获取状态类型
    getStatusType(status) {
        switch(status) {
            case '在线': return 'success';
            case '离线': return 'danger';
            case '警告': return 'warning';
            default: return 'info';
        }
    },
    
    // 获取状态类名
    getStatusClass(status) {
        switch(status) {
            case '在线': return 'online';
            case '离线': return 'offline';
            case '警告': return 'warning';
            default: return 'info';
        }
    },
    
    // 获取时间差样式类
    getTimeDiffClass(timeDiff) {
        if (!timeDiff) return '';
        if (timeDiff > 60) return 'status-warning';
        return 'status-online';
    }
};

// 导出到全局
window.CommonUtils = CommonUtils;
window.AuthManager = AuthManager; 